home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Examples / Helpfile / GUIRegisterMsg.au3 < prev    next >
Text File  |  2007-09-08  |  13KB  |  413 lines

  1. ; *******************************************************
  2. ; Example - Create an ownerdrawn/colored button
  3. ; *******************************************************
  4.  
  5. #include <GUIConstants.au3>
  6.  
  7. Global Const $WM_DRAWITEM            = 0x002B
  8. Global Const $WM_COMMAND            = 0x0111
  9.  
  10. Global Const $GWL_STYLE             = -16
  11.  
  12. Global Const $BS_OWNERDRAW            = 0x0000000B
  13.  
  14. Global Const $COLOR_BTNTEXT            = 18
  15. Global Const $COLOR_BTNFACE            = 15
  16. Global Const $COLOR_BTNSHADOW        = 16
  17. Global Const $COLOR_HIGHLIGHTTEXT    = 14
  18. Global Const $COLOR_GRAYTEXT        = 17
  19.  
  20. Global Const $DT_CENTER                = 0x00000001
  21. Global Const $DT_RIGHT                = 0x00000002
  22. Global Const $DT_VCENTER            = 0x00000004
  23. Global Const $DT_BOTTOM                = 0x00000008
  24. Global Const $DT_WORDBREAK            = 0x00000010
  25. Global Const $DT_SINGLELINE            = 0x00000020
  26. Global Const $DT_EXPANDTABS            = 0x00000040
  27. Global Const $DT_TABSTOP            = 0x00000080
  28. Global Const $DT_NOCLIP                = 0x00000100
  29. Global Const $DT_EXTERNALLEADING    = 0x00000200
  30. Global Const $DT_CALCRECT            = 0x00000400
  31. Global Const $DT_NOPREFIX            = 0x00000800
  32. Global Const $DT_INTERNAL            = 0x00001000
  33.  
  34. Global Const $ODS_SELECTED            = 0x0001
  35. Global Const $ODS_GRAYED            = 0x0002
  36. Global Const $ODS_DISABLED            = 0x0004
  37. Global Const $ODS_CHECKED            = 0x0008
  38. Global Const $ODS_FOCUS                = 0x0010
  39. Global Const $ODS_HOTLIGHT            = 0x0040
  40. Global Const $ODS_INACTIVE            = 0x0080
  41. Global Const $ODS_NOACCEL            = 0x0100
  42. Global Const $ODS_NOFOCUSRECT        = 0x0200
  43.  
  44. Global Const $ODT_BUTTON            = 4
  45.  
  46. Global Const $DFC_BUTTON            = 4
  47. Global Const $DFCS_BUTTONPUSH        = 0x0010
  48.  
  49.  
  50. $hGUI        = GUICreate("My Ownerdrawn Created Button", 300, 200)
  51.  
  52. $nButton    = GUICtrlCreateButton("", 90, 50, 120, 30)
  53. GUICtrlSetStyle($nButton, BitOr($WS_TABSTOP, $BS_NOTIFY, $BS_OWNERDRAW)) ; Set the ownerdrawn flag
  54.  
  55. $nButton2    = GUICtrlCreateButton("Normal Button", 90, 110, 120, 30)
  56.  
  57. GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
  58. ; WM_DRAWITEM has to registered before showing GUI otherwise the initial drawing isn't done
  59. GUIRegisterMsg($WM_DRAWITEM, "MY_WM_DRAWITEM")
  60.  
  61. GUISetState()
  62.  
  63. While 1
  64.     $GUIMsg = GUIGetMsg()
  65.     
  66.     Switch $GUIMsg
  67.         Case $GUI_EVENT_CLOSE
  68.             ExitLoop
  69.             
  70.         Case $nButton
  71.             ; Normally should not run through cause of our MY_WM_COMMAND function
  72.             Msgbox(0, "Info", "Button pressed") 
  73.             
  74.         Case $nButton2
  75.             ; Normally should not run through cause of our MY_WM_COMMAND function
  76.             Msgbox(0, "Info", "Button2 pressed")
  77.     EndSwitch
  78. WEnd
  79.  
  80. Exit
  81.  
  82.  
  83. ; React on a button click
  84. Func MY_WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
  85.     $nNotifyCode    = BitShift($wParam, 16)
  86.     $nID            = BitAnd($wParam, 0x0000FFFF)
  87.     $hCtrl            = $lParam
  88.     
  89.     If $nID <> 2 And $nNotifyCode = 0 Then ; Check for IDCANCEL - 2
  90.         ; Ownerdrawn buttons don't send something by pressing ENTER
  91.         ; So IDOK - 1 comes up, now check for the control that has the current focus
  92.         If $nID = 1 Then
  93.             $hFocus = DllCall("user32.dll", "hwnd", "GetFocus")
  94.             $nCtrlID = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hFocus[0])
  95.             PostButtonClick($hFocus[0], $nCtrlID[0])
  96.         Else
  97.             Msgbox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hwnd & @LF & _
  98.                                         "MsgID" & @TAB & ":" & $Msg & @LF & _
  99.                                         "wParam" & @TAB & ":" & $wParam & @LF & _
  100.                                         "lParam" & @TAB & ":" & $lParam & @LF & @LF & _
  101.                                         "WM_COMMAND - Infos:" & @LF & _
  102.                                         "-----------------------------" & @LF & _
  103.                                         "Code" & @TAB & ":" & $nNotifyCode & @LF & _
  104.                                         "CtrlID" & @TAB & ":" & $nID & @LF & _
  105.                                         "CtrlHWnd" & @TAB & ":" & $hCtrl)
  106.         EndIf
  107.         Return 0 ; Only workout clicking on the button
  108.     EndIf
  109.     ; Proceed the default Autoit3 internal message commands.
  110.     ; You also can complete let the line out.
  111.     ; !!! But only 'Return' (without any value) will not proceed
  112.     ; the default Autoit3-message in the future !!!
  113.     Return $GUI_RUNDEFMSG
  114. EndFunc
  115.  
  116.  
  117. ; RePost a WM_COMMAND message to a ctrl in a gui window
  118. Func PostButtonClick($hWnd, $nCtrlID)
  119.     DllCall("user32.dll", "int", "PostMessage", _
  120.                                     "hwnd", $hGUI, _
  121.                                     "int", $WM_COMMAND, _
  122.                                     "int", BitAnd($nCtrlID, 0x0000FFFF), _
  123.                                     "hwnd", GUICtrlGetHandle($nCtrlid))
  124. EndFunc
  125.  
  126.  
  127. ; Draw the button
  128. Func MY_WM_DRAWITEM($hWnd, $Msg, $wParam, $lParam)
  129.     Local $stDrawItem = DllStructCreate("uint;uint;uint;uint;uint;uint;uint;int[4];dword", $lParam)
  130.     
  131.     $nCtlType = DllStructGetData($stDrawItem, 1)
  132.     If $nCtlType = $ODT_BUTTON Then
  133.         $nCtrlID    = DllStructGetData($stDrawItem, 2)
  134.         $nItemState    = DllStructGetData($stDrawItem, 5)
  135.         $hCtrl        = DllStructGetData($stDrawItem, 6)
  136.         $hDC        = DllStructGetData($stDrawItem, 7)
  137.         $nLeft        = DllStructGetData($stDrawItem, 8, 1)
  138.         $nTop        = DllStructGetData($stDrawItem, 8, 2)
  139.         $nRight        = DllStructGetData($stDrawItem, 8, 3)
  140.         $nBottom    = DllStructGetData($stDrawItem, 8, 4)
  141.         $sText        = "Ownerdrawn Button"
  142.         $nTextColor    = 0x5555DD
  143.         $nBackColor    = 0xFFEEDD
  144.         DrawButton($hWnd, $hCtrl, $hDC, $nLeft, $nTop, $nRight, $nBottom, $nItemState, $sText, $nTextColor, $nBackColor)
  145.         $stDrawItem = 0
  146.         Return 1
  147.     EndIf
  148.  
  149.     $stDrawItem = 0
  150.     Return $GUI_RUNDEFMSG ; Proceed the default Autoit3 internal message commands
  151. EndFunc
  152.  
  153.  
  154. ; The main drawing procedure
  155. Func DrawButton($hWnd, $hCtrl, $hDC, $nLeft, $nTop, $nRight, $nBottom, $nItemState, $sText, $nTextColor, $nBackColor)
  156.     ;Local $bDefault    = FALSE
  157.     Local $bChecked    = BitAnd($nItemState, $ODS_CHECKED)
  158.     Local $bFocused    = BitAnd($nItemState, $ODS_FOCUS)
  159.     Local $bGrayed    = BitAnd($nItemState, BitOr($ODS_GRAYED, $ODS_DISABLED))
  160.     Local $bSelected= BitAnd($nItemState, $ODS_SELECTED)
  161.  
  162.     $stRect = DllStructCreate("int;int;int;int")
  163.     DllStructSetData($stRect, 1, $nLeft)
  164.     DllStructSetData($stRect, 2, $nTop)
  165.     DllStructSetData($stRect, 3, $nRight)
  166.     DllStructSetData($stRect, 4, $nBottom)
  167.     
  168.     If $bGrayed Then
  169.         $nClrTxt    = SetTextColor($hDC, GetSysColor($COLOR_HIGHLIGHTTEXT))
  170.     ElseIf $nTextColor = -1 Then
  171.         $nClrTxt    = SetTextColor($hDC, GetSysColor($COLOR_BTNTEXT))
  172.     Else
  173.         $nClrTxt    = SetTextColor($hDC, $nTextColor)
  174.     EndIf
  175.     
  176.     If $nBackColor = -1 Then
  177.         $hBrush        = GetSysColorBrush($COLOR_BTNFACE)
  178.         $nClrSel    = GetSysColor($COLOR_BTNFACE)
  179.     Else
  180.         $hBrush        = CreateSolidBrush($nBackColor)
  181.         $nClrSel    = $nBackColor;
  182.     EndIf
  183.  
  184.     $nClrBk            = SetBkColor($hDC, $nClrSel)
  185.     $hOldBrush        = SelectObject($hDC, $hBrush)
  186.  
  187.     $nTmpLeft    = $nLeft
  188.     $nTmpTop    = $nTop
  189.     $nTmpRight    = $nRight
  190.     $nTmpBottom    = $nBottom
  191.     
  192.     If $bSelected Then
  193.         InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -1, -1)
  194.         $hBrushSel = CreateSolidBrush(GetSysColor($COLOR_BTNSHADOW))
  195.         FrameRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $hBrushSel)
  196.         DeleteObject($hBrushSel)
  197.     Else
  198.         If $bFocused And Not $bSelected Then InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -1, -1)
  199.         DrawFrameControl($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $DFC_BUTTON, $DFCS_BUTTONPUSH)
  200.     EndIf
  201.     
  202.     $nTmpLeft    = $nLeft
  203.     $nTmpTop    = $nTop
  204.     $nTmpRight    = $nRight
  205.     $nTmpBottom    = $nBottom
  206.     
  207.     If $bSelected Then
  208.         InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -2, -2)
  209.     Else
  210.         If $bFocused And Not $bSelected Then
  211.             InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -3, -3)
  212.             $nTmpLeft -= 1
  213.             $nTmpTop -= 1
  214.         Else
  215.             InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -2, -2)
  216.             $nTmpLeft -= 1
  217.             $nTmpTop -= 1
  218.         EndIf
  219.     EndIf
  220.     
  221.     FillRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $hBrush)
  222.     
  223.     If $bSelected Or $bGrayed Then
  224.         $nTmpLeft = $nTmpLeft + 2
  225.         $nTmpTop = $nTmpTop +2
  226.     EndIf
  227.     
  228.     $uFlags = BitOr($DT_NOCLIP, $DT_CENTER, $DT_VCENTER)
  229.  
  230.     If Not BitAnd(GetWindowLong($hCtrl, $GWL_STYLE), $BS_MULTILINE) Then $uFlags = BitOr($uFlags, $DT_SINGLELINE)
  231.     
  232.     DrawText($hDC, $sText, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $uFlags)
  233.  
  234.     If $bGrayed Then
  235.         $nTmpLeft    = $nLeft
  236.         $nTmpTop    = $nTop
  237.         $nTmpRight    = $nRight
  238.         $nTmpBottom    = $nBottom
  239.         
  240.         $nTmpLeft -= 1
  241.         
  242.         $nClrTxt    = SetTextColor($hDC, GetSysColor($COLOR_GRAYTEXT))
  243.         DrawText($hDC, $sText, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, BitOr($DT_NOCLIP, $DT_CENTER, $DT_VCENTER, $DT_SINGLELINE))
  244.     EndIf
  245.  
  246.     $nTmpLeft    = $nLeft
  247.     $nTmpTop    = $nTop
  248.     $nTmpRight    = $nRight
  249.     $nTmpBottom    = $nBottom
  250.         
  251.     If $bFocused Then
  252.         $hBrush    = CreateSolidBrush(0)
  253.         FrameRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, $hBrush)
  254.         
  255.         $nTmpLeft    = $nLeft
  256.         $nTmpTop    = $nTop
  257.         $nTmpRight    = $nRight
  258.         $nTmpBottom    = $nBottom
  259.         
  260.         InflateRect($nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom, -4, -4)
  261.         DrawFocusRect($hDC, $nTmpLeft, $nTmpTop, $nTmpRight, $nTmpBottom)
  262.     EndIf
  263.  
  264.     SelectObject($hDC, $hOldBrush)
  265.     DeleteObject($hBrush)
  266.     SetTextColor($hDC, $nClrTxt)
  267.     SetBkColor($hDC, $nClrBk)
  268.          
  269.     Return 1
  270. EndFunc
  271.  
  272.  
  273. ; Some graphic / windows functions
  274. Func CreateSolidBrush($nColor)
  275.     Local $hBrush = DllCall("gdi32.dll", "hwnd", "CreateSolidBrush", "int", $nColor)
  276.     Return $hBrush[0]
  277. EndFunc
  278.  
  279.  
  280. Func GetSysColor($nIndex)
  281.     Local $nColor = DllCall("user32.dll", "int", "GetSysColor", "int", $nIndex)
  282.     Return $nColor[0]
  283. EndFunc
  284.  
  285.  
  286. Func GetSysColorBrush($nIndex)
  287.     Local $hBrush = DllCall("user32.dll", "hwnd", "GetSysColorBrush", "int", $nIndex)
  288.     Return $hBrush[0]
  289. EndFunc
  290.  
  291.  
  292. Func DrawFrameControl($hDC, $nLeft, $nTop, $nRight, $nBottom, $nType, $nState)
  293.     Local     $stRect = DllStructCreate("int;int;int;int")
  294.     
  295.     DllStructSetData($stRect, 1, $nLeft)
  296.     DllStructSetData($stRect, 2, $nTop)
  297.     DllStructSetData($stRect, 3, $nRight)
  298.     DllStructSetData($stRect, 4, $nBottom)
  299.     
  300.     DllCall("user32.dll", "int", "DrawFrameControl", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "int", $nType, "int", $nState)
  301.  
  302.     $stRect = 0
  303. EndFunc
  304.  
  305.  
  306. Func DrawFocusRect($hDC, $nLeft, $nTop, $nRight, $nBottom)
  307.     Local     $stRect = DllStructCreate("int;int;int;int")
  308.     
  309.     DllStructSetData($stRect, 1, $nLeft)
  310.     DllStructSetData($stRect, 2, $nTop)
  311.     DllStructSetData($stRect, 3, $nRight)
  312.     DllStructSetData($stRect, 4, $nBottom)
  313.     
  314.     DllCall("user32.dll", "int", "DrawFocusRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect))
  315.     
  316.     $stRect = 0
  317. EndFunc
  318.  
  319.  
  320. Func DrawText($hDC, $sText, $nLeft, $nTop, $nRight, $nBottom, $nFormat)
  321.     Local $nLen = StringLen($sText)
  322.     
  323.     Local $stRect = DllStructCreate("int;int;int;int")
  324.     DllStructSetData($stRect, 1, $nLeft)
  325.     DllStructSetData($stRect, 2, $nTop)
  326.     DllStructSetData($stRect, 3, $nRight)
  327.     DllStructSetData($stRect, 4, $nBottom)
  328.     
  329.     Local $stText = DllStructCreate("char[260]")
  330.     DllStructSetData($stText, 1, $sText)
  331.     
  332.     DllCall("user32.dll", "int", "DrawText", "hwnd", $hDC, "ptr", DllStructGetPtr($stText), "int", $nLen, "ptr", DllStructGetPtr($stRect), "int", $nFormat)
  333.     
  334.     $stRect = 0
  335.     $stText    = 0
  336. EndFunc
  337.  
  338.  
  339. Func FillRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
  340.     Local     $stRect = DllStructCreate("int;int;int;int")
  341.     
  342.     DllStructSetData($stRect, 1, $nLeft)
  343.     DllStructSetData($stRect, 2, $nTop)
  344.     DllStructSetData($stRect, 3, $nRight)
  345.     DllStructSetData($stRect, 4, $nBottom)
  346.     
  347.     DllCall("user32.dll", "int", "FillRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
  348.     
  349.     $stRect = 0
  350. EndFunc
  351.  
  352.  
  353. Func FrameRect($hDC, $nLeft, $nTop, $nRight, $nBottom, $hBrush)
  354.     Local     $stRect = DllStructCreate("int;int;int;int")
  355.     
  356.     DllStructSetData($stRect, 1, $nLeft)
  357.     DllStructSetData($stRect, 2, $nTop)
  358.     DllStructSetData($stRect, 3, $nRight)
  359.     DllStructSetData($stRect, 4, $nBottom)
  360.     
  361.     DllCall("user32.dll", "int", "FrameRect", "hwnd", $hDC, "ptr", DllStructGetPtr($stRect), "hwnd", $hBrush)
  362.     
  363.     $stRect = 0
  364. EndFunc
  365.  
  366.  
  367. Func InflateRect(ByRef $nLeft, ByRef $nTop, ByRef $nRight, ByRef $nBottom, $nX, $nY)
  368.     Local     $stRect = DllStructCreate("int;int;int;int")
  369.     
  370.     DllStructSetData($stRect, 1, $nLeft)
  371.     DllStructSetData($stRect, 2, $nTop)
  372.     DllStructSetData($stRect, 3, $nRight)
  373.     DllStructSetData($stRect, 4, $nBottom)
  374.     
  375.     DllCall("user32.dll", "int", "InflateRect", "ptr", DllStructGetPtr($stRect), "int", $nX, "int", $nY)
  376.     
  377.     $nLeft        = DllStructGetData($stRect, 1)
  378.     $nTop        = DllStructGetData($stRect, 2)
  379.     $nRight        = DllStructGetData($stRect, 3)
  380.     $nBottom    = DllStructGetData($stRect, 4)
  381.     
  382.     $stRect = 0
  383. EndFunc
  384.  
  385.  
  386. Func SetBkColor($hDC, $nColor)
  387.     Local $nOldColor = DllCall("gdi32.dll", "int", "SetBkColor", "hwnd", $hDC, "int", $nColor)
  388.     Return $nOldColor[0]
  389. EndFunc
  390.  
  391.  
  392. Func SetTextColor($hDC, $nColor)
  393.     Local $nOldColor = DllCall("gdi32.dll", "int", "SetTextColor", "hwnd", $hDC, "int", $nColor)
  394.     Return $nOldColor[0]
  395. EndFunc
  396.  
  397.  
  398. Func SelectObject($hDC, $hObj)
  399.     Local $hOldObj = DllCall("gdi32.dll", "hwnd", "SelectObject", "hwnd", $hDC, "hwnd", $hObj)
  400.     Return $hOldObj[0]
  401. EndFunc
  402.  
  403.  
  404. Func DeleteObject($hObj)
  405.     Local $nResult = DllCall("gdi32.dll", "hwnd", "DeleteObject", "hwnd", $hObj)
  406. EndFunc
  407.  
  408.  
  409. Func GetWindowLong($hWnd, $nIndex)
  410.     Local $nVal = DllCall("user32.dll", "int", "GetWindowLong", "hwnd", $hWnd, "int", $nIndex)
  411.     Return $nVal[0]
  412. EndFunc
  413.